package com.cma.cmabackend.service;

import com.cma.cmabackend.dao.EquipmentDao;
import com.cma.cmabackend.entity.Equipment;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentService {
    @Resource
    EquipmentDao equipmentDao;

    /**
     * 添加设备
     *
     * @param equipmentList 新设备
     */
    public void addEquipments(List<Equipment> equipmentList) {
        equipmentDao.saveAll(equipmentList);
    }

    public Map<String, Object> addEquipment(Equipment equipment) {
        Map<String, Object> result = new HashMap<>();
        equipmentDao.save(equipment);
        result.put("success", true);
        return result;
    }

    /**
     * 设备分页查询
     *
     * @param page         页码
     * @param per_page_num 每页数量
     * @return 页面对象
     */
    public Page<Equipment> pageQuery(int page, int per_page_num) {
        Pageable pageable = PageRequest.of(page - 1, per_page_num);
        return equipmentDao.findAll(pageable);
    }

    public List<Equipment> getAll() {
        return equipmentDao.findAll();
    }

    public Equipment findById(int id) {
        return equipmentDao.findById(id).orElse(null);
    }
}
